package com.jspgou.cms.action.member;

import static com.jspgou.cms.Constants.MEMBER_SYS;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.entity.Discuss;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.DiscussMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.security.annotation.Secured;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.front.URLHelper;

@Secured
@Controller
public class DiscussFroAct {
	private static final Logger log = LoggerFactory.getLogger(DiscussFroAct.class);
	/**
	 * 评价管理
	 */
	private static final String TPL_DISSCUSS = "tpl.discuss";
	/**
	 * 查看评价
	 */
	private static final String TPL_VIEW_DISSCUSS="tpl.viewDiscuss";
	/**
	 * 立即评价
	 */
	private static final String TPL_QUICK_DISSCUSS="tpl.quickDiscuss";
	

	// 我的评论（wang ze wu）会员中心的
	@RequestMapping(value = "/discuss/discuss*.jspx")
	public String getDiscuss(String cl,HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		model.addAttribute("historyProductIds", "");
		Cookie[] cookie = request.getCookies();
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("shop_record")){
				String str = cookie[i].getValue();
				model.addAttribute("historyProductIds", str);
				break;
			}
		}
		model.addAttribute("cl", cl);
		model.addAttribute("member", member);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				TPL_DISSCUSS), request);
	}
	
	@RequestMapping(value = "/discuss/viewDiscuss.jspx")
	public String viewDiscuss(Long id, Integer pageNo, 
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Discuss bean =manager.findById(id);
		model.addAttribute("bean", bean);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("member", member);
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				TPL_VIEW_DISSCUSS), request);
	}
	
	@RequestMapping(value = "/discuss/quickDiscuss.jspx")
	public String quickDiscuss(Long id,Integer pageNo, 
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		model.addAttribute("member", member);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("bean", manager.findById(id));
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				TPL_QUICK_DISSCUSS), request);
	}
	
	@RequestMapping(value = "/discuss/updateDiscuss.jspx")
	public String updateDiscuss(Discuss bean,Integer pageNo,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		bean.setCreateTime(new Date());
		manager.update(bean);
		model.addAttribute("member", member);
		model.addAttribute("pageNo", pageNo);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,TPL_DISSCUSS), request);
	}

	@Autowired
	private DiscussMng manager;
}
